"""
@Project:workflow_python
@File:serializers.py
@Author:袁浩天
@Date:9:11
"""

from .models import *
from rest_framework import serializers
from rest_framework.validators import ValidationError

import re


# 审批层次
class LevelSer(serializers.ModelSerializer):
    # currentvetting = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    # reviselast = serializers.SerializerMethodField()
    #
    # def get_reviselast(self, obj):
    #     return obj.currentvetting.name

    class Meta:
        model = LevelModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 类别
class TypeSer(serializers.ModelSerializer):
    lastervisename = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    reviselast = serializers.SerializerMethodField()

    def get_reviselast(self,obj):
        return obj.lastervisename.name

    class Meta:
        model = TypeModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 流程表
class FlowSer(serializers.ModelSerializer):
    department = serializers.PrimaryKeyRelatedField(queryset=DepartmentModel.objects.all())
    department_name = serializers.SerializerMethodField()

    currentvetting = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    reviselast = serializers.SerializerMethodField()

    def get_reviselast(self, obj):
        return obj.currentvetting.name

    def get_department_name(self,obj):
        return obj.department.name

    class Meta:
        model = FlowModel
        fields = "__all__"

    def validate(self, attrs):
        name = attrs.get("flow_name")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 流程节点表
class FlowNodeSer(serializers.ModelSerializer):
    user_name = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    name = serializers.SerializerMethodField()

    vettypes = serializers.PrimaryKeyRelatedField(queryset=UserModel.objects.all())
    vettype = serializers.SerializerMethodField()

    def get_name(self,obj):
        return obj.user_name.name

    def get_vettype(self,obj):
        return obj.vettypes.name

    class Meta:
        model = FlowNodeModel
        fields = "__all__"

    # def validate(self, attrs):
    #     name = attrs.get("node_name")
    #     examination_why = attrs.get("examination_why")
    #     why = attrs.get("why")
    #
    #     if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
    #         raise ValidationError(f'{name} 名称由2到30位汉字、数字、字母或下划线组成！')
    #
    #     if not re.findall(r"^[\u4e00-\u9fa5\w]{3, 50}", examination_why):
    #         raise ValidationError(f'{examination_why} 审批原因由3到50、数字、字母或下划线组成！')
    #
    #     if not re.findall(r"^[\u4e00-\u9fa5\w]{3, 50}", why):
    #         raise ValidationError(f'{examination_why} 请求由3到50、数字、字母或下划线组成！')
    #
    #     return attrs


# 用户流程节点表
class UserFlowNodeSer(serializers.ModelSerializer):
    class Meta:
        model = UserFlowNodeModel
        fields = "__all__"


# 节点信息表
class NodeInformationSer(serializers.ModelSerializer):
    class Meta:
        model = NodeInformationModel
        fields = "__all__"

    def validate(self,attrs):
        name = attrs.get("describe")

        if not re.findall(r"^[\u4e00-\u9fa5\w]{2,30}$",name):
            raise ValidationError(f'{name} 描述由2到30位汉字、数字、字母或下划线组成！')

        return attrs


# 历史审批
class HistoryExaminationSer(serializers.ModelSerializer):
    class Meta:
        model = HistoryExaminationModel
        fields = "__all__"





